package com.zkx.fssb.controller.admin;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zkx.fssb.domain.dto.EmployeeDto;
import com.zkx.fssb.domain.po.Employee;
import com.zkx.fssb.domain.po.Flower;
import com.zkx.fssb.domain.vo.EmployeeVo;
import com.zkx.fssb.result.PageQuery;
import com.zkx.fssb.result.Result;
import com.zkx.fssb.service.EmployeeService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/admin/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @GetMapping("/page")
    public Result<Page> page(PageQuery pageQuery){

        Page pageInfo = employeeService.customPage(pageQuery);
        return Result.success(pageInfo);

    }

    /**
     * 新建员工
     * @param employeeDto
     * @return
     */
    @PostMapping
    public Result saveEmployee(@RequestBody EmployeeDto employeeDto){
        employeeService.customeSaveEmployee(employeeDto);
        return Result.success("新建员工成功");

    }

    /**
     * 更新员工
     * @param employeeDto
     * @return
     */
    @PutMapping
    public Result updateEmployee(@RequestBody EmployeeDto employeeDto){
        employeeService.customUpdateEmployee(employeeDto);
        return Result.success("更新员工成功");
    }

    @DeleteMapping("/{id}")
    public Result removeEmployee(@PathVariable Integer id){
        employeeService.removeById(id);
        return Result.success("删除员工成功");

    }


    /**
     * 员工登录
     * @param employeeDto
     * @return
     */
    @PostMapping("/login")
    public Result<EmployeeVo> login(@RequestBody EmployeeDto employeeDto){
        log.info(employeeDto.toString());
        EmployeeVo employeeVo = employeeService.customLogin(employeeDto);
        return Result.success(employeeVo);

    }




    @PostMapping("/logout")
    public Result logout(){

        return null;

    }

    @GetMapping("/info")
    public ResponseEntity<TestResponse> login(String token) {
        TestResponse response = new TestResponse();
        TestData data = new TestData();
        if(token !="admin-token"){
            response.setCode(20000);
            data.setRoles(Arrays.asList("admin"));
            data.setIntroduction("I am a super administrator");
            data.setName("Super Admin");
            response.setData(data);}

        return ResponseEntity.ok(response);
    }

    @GetMapping("/getById")
    public Result<Employee> getById(Integer employeeId){
        Employee employee = employeeService.getById(employeeId);

        return Result.success(employee);

    }

}

@Data
class TestResponse {
    private int code;
    private TestData data;

    // getters, setters...
}

@Data
class TestData {
    private List<String> roles;
    private String introduction;
    private String avatar;
    private String name;
}


